package com.eliaukjq.xunlianying.duoxianchengjichu;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2024/7/20 - 19:18
 */
public class WaitExample {
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("Thread 1 is waiting for 3 seconds...");
                    lock.wait(10000); // 等待3秒钟或直到被通知
                    System.out.println("Thread 1 is done waiting.");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                try {
                    Thread.sleep(1000); // 等待1秒钟后通知
                    lock.notify();
                    System.out.println("Thread 2 sent notification.");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}
